const query = require('../../dbConfig/index')
const {atDate} = require('../../utils/utils')
const get = async (ctx,next)=>{	
	const res = await query(`select * from article_class`)
	if(res.length>0){
		ctx.body={code:200,data:res,msg:'成功'}
		next()
	}else{
		ctx.body={code:0,data:null,msg:'失败'}
	}
}
const updata = async (ctx,next)=>{
	const body = ctx.request.body
	if(!body.id && !body.title){
		ctx.body={code:0,data:null,msg:'缺少数据'}
		return
	}
	await query(`UPDATE article_class SET class_title = ? WHERE Id='${body.id}'`,[body.title])
	ctx.body={code:200,data:null,msg:'成功'}
	next()
}
const add = async (ctx,next)=>{
	const body = ctx.request.body
	await query(`INSERT INTO article_class (class_title) VALUES (?)`,[body.title])
	ctx.body={code:200,data:null,msg:'成功'}
	next()
}
const remove = async (ctx,next)=>{
	const body = ctx.request.body
	const res = await query(`select * from img_txt_content WHERE classId=${body.id}`)
	if(res.length){
		ctx.body={code:0,data:null,msg:'该分类下有未删除的文章,删除失败'}
		return
	}
	await query(`DELETE FROM article_class WHERE Id=${body.id}`)
	ctx.body={code:200,data:null,msg:'成功'}
	next()
}
const upload = async (ctx,next)=>{
	ctx.status = 200
	const data = 'http://'+ctx.request.header.host+'/images/'+ctx.request.files.file.newFilename
	ctx.body = {code:200,data,msg:'成功'}
	next()
}
const editorUpload = async (ctx,next)=>{
	ctx.status = 200
	const data = 'http://'+ctx.request.header.host+'/images/'+ctx.request.files.file.newFilename
	ctx.body = JSON.stringify({errno:0,data:[data]})
	next()
}
const addArticle = async (ctx,next)=>{
	const body = ctx.request.body
	const author = ctx.state.user
	const create_time = atDate()
	console.log(create_time)
	await query(`INSERT INTO img_txt_content (article_title,classId,article_author,create_time,article_state,content_body,main_pic) VALUES (?,?,?,?,?,?,?)`,[body.title,body.classId,author,create_time,0,body.content_body,body.main_pic])
	ctx.body={code:200,data:null,msg:'成功'}
	next()
}
const getArticle = async (ctx,next)=>{
	const body = ctx.request.body
	const offset = (body.pageNumber - 1) * Number(body.pageSize);  
	let sql = "select * from img_txt_content"
	let params = [];  
	// 如果传了条件 就拼接sql
	if (body.title && body.title.trim() !== '') {  
		sql += ' WHERE article_title=?';  
		params.push(body.title);  
	} 
	if (body.create_time) {  
		sql +=sql.includes('WHERE')?' and create_time=?': ' WHERE create_time=?';  
		params.push(body.create_time);  
	} 
	if (body.classId) {  
		sql +=sql.includes('WHERE')?' and classId=?': ' WHERE classId=?';  
		params.push(body.classId);  
	} 
	if (body.author && body.author.trim() !== '') {  
		sql +=sql.includes('WHERE')?' and article_author=?': ' WHERE article_author=?';  
		params.push(body.author);  
	} 
	sql += ' LIMIT ? OFFSET ?';  
	params.push(Number(body.pageSize));  
	params.push(offset);
	const res = await query(sql,params)
	const total = await query('SELECT COUNT(*) as total FROM img_txt_content')
	ctx.body={code:200,data:res,total:total[0].total,msg:'成功'}
	next()
}
const removeArticle = async (ctx,next) =>{
	const body = ctx.request.body
	await query(`DELETE FROM img_txt_content WHERE Id=${body.id}`)
	ctx.body={code:200,data:null,msg:'成功'}
	next()
}
const updataArticle = async (ctx,next) =>{
	const body = ctx.request.body
	await query(`UPDATE img_txt_content SET article_title=?,classId=?,content_body=?,main_pic=? WHERE Id='${body.Id}'`,[body.title,body.classId,body.content_body,body.main_pic])
	ctx.body={code:200,data:null,msg:'成功'}
	next()
}
const lookArticle = async (ctx,next) =>{
	const body = ctx.request.body
	const res = await query(`select * from img_txt_content WhERE Id=${body.id}`)
	ctx.body={code:200,data:res,msg:'成功'}
	next()
}
const changeArticleState = async (ctx,next) => {
	const body = ctx.request.body
	await query(`UPDATE img_txt_content SET article_state=? WHERE Id='${body.Id}'`,[body.state])
	ctx.body={code:200,data:null,msg:'成功'}
	next()
}
const updataPassword = async (ctx,next) => {
	const body = ctx.request.body
	const getSql = body.power!=1?`select * from teacher where teacher_id='${body.id}'`:`select * from user where id=${body.id}`
	const d = await query(getSql)
	if(d.length){
		const pass = body.power!=1?d[0].teacher_password:d[0].password
		if(pass!==body.oldPass){
			ctx.body={code:0,data:null,msg:'原始密码错误'}
			return
		}
	}
	const updataSql = body.power!=1?`UPDATE teacher SET teacher_password = ? WHERE teacher_id='${body.id}'`:`UPDATE user SET 	password = ? WHERE id=${body.id}`
	await query(updataSql,[body.password])
	ctx.body={code:200,data:null,msg:'成功'}
	next()
}
module.exports = {
	editorUpload,
	upload,
	get,
	add,updata,remove,
	addArticle,
	getArticle,
	removeArticle,
	updataArticle,
	lookArticle,
	changeArticleState,
	updataPassword
}